package org.example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RomanNumber {



    static Map<String,Integer> simpleNumberMapping = new HashMap<String,Integer>();
    static Map<String,Integer> complexNumberMapping = new HashMap<String,Integer>();


    public static int romanToInt(String s) {

        simpleNumberMapping.put("I", 1);
        simpleNumberMapping.put("V", 5);
        simpleNumberMapping.put("X", 10);
        simpleNumberMapping.put("L", 50);
        simpleNumberMapping.put("C", 100);
        simpleNumberMapping.put("D", 500);
        simpleNumberMapping.put("M", 1000);
        complexNumberMapping.put("IV", 4);
        complexNumberMapping.put("IX", 9);
        complexNumberMapping.put("XL", 40);
        complexNumberMapping.put("XC", 90);
        complexNumberMapping.put("CD", 400);
        complexNumberMapping.put("CM", 900);
        List<Integer> list = new ArrayList<>();
        recursionFunc(s,list);
        return list.stream().mapToInt(i -> i).reduce(0, Integer::sum);
    }


    public static int recursionFunc(String romamNumber,  List<Integer> list ) {

        for (String key : complexNumberMapping.keySet()) {
            int i = romamNumber.indexOf(key.toUpperCase());
            if (i != -1) {
                Integer value = complexNumberMapping.get(key);
                romamNumber = romamNumber.replaceFirst(key, "");
                list.add(value);
            }

        }

        for (String key : simpleNumberMapping.keySet()) {
            int i = romamNumber.indexOf(key.toUpperCase());
            if (i != -1) {
                Integer value = simpleNumberMapping.get(key);
                romamNumber = romamNumber.replaceFirst(key, "");
                list.add(value);
            }
        }

        if (romamNumber.length() >= 1) {
            recursionFunc(romamNumber,list);
        }
        return 0;
    }

    public static void main(String[] args) {
        System.out.println(romanToInt("III")) ;
    }
}
